﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Bangle.DBUtility;
namespace Bangle.DAL
{
    //T_Node
    public partial class T_NodeDAL
    {

        public bool Exists(Guid NodeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Node");
            strSql.Append(" where ");
            strSql.Append(" NodeId = @NodeId  ");
            SqlParameter[] parameters = {
					new SqlParameter("@NodeId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = NodeId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Bangle.Model.T_Node model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Node(");
            strSql.Append("NodeId,Title,Content,RemindTime,OrderTime,CreateTime,CreateUserId,UpdateTime,UpdateUserId,Valid");
            strSql.Append(") values (");
            strSql.Append("@NodeId,@Title,@Content,@RemindTime,@OrderTime,@CreateTime,@CreateUserId,@UpdateTime,@UpdateUserId,@Valid");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@NodeId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@Title", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Content", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@RemindTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@OrderTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.NodeId;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.RemindTime;
            parameters[4].Value = model.OrderTime;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.CreateUserId;
            parameters[7].Value = model.UpdateTime;
            parameters[8].Value = model.UpdateUserId;
            parameters[9].Value = model.Valid;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Bangle.Model.T_Node model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Node set ");

            strSql.Append(" NodeId = @NodeId , ");
            strSql.Append(" Title = @Title , ");
            strSql.Append(" Content = @Content , ");
            strSql.Append(" RemindTime = @RemindTime , ");
            strSql.Append(" OrderTime = @OrderTime , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" CreateUserId = @CreateUserId , ");
            strSql.Append(" UpdateTime = @UpdateTime , ");
            strSql.Append(" UpdateUserId = @UpdateUserId , ");
            strSql.Append(" Valid = @Valid  ");
            strSql.Append(" where NodeId=@NodeId  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@NodeId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@Title", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@Content", SqlDbType.VarChar,8000) ,            
                        new SqlParameter("@RemindTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@OrderTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.NodeId;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.RemindTime;
            parameters[4].Value = model.OrderTime;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.CreateUserId;
            parameters[7].Value = model.UpdateTime;
            parameters[8].Value = model.UpdateUserId;
            parameters[9].Value = model.Valid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid NodeId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Node ");
            strSql.Append(" where NodeId=@NodeId ");
            SqlParameter[] parameters = {
					new SqlParameter("@NodeId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = NodeId;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Bangle.Model.T_Node GetModel(Guid NodeId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NodeId, Title, Content, RemindTime, OrderTime, CreateTime, CreateUserId, UpdateTime, UpdateUserId, Valid  ");
            strSql.Append("  from T_Node ");
            strSql.Append(" where NodeId=@NodeId ");
            SqlParameter[] parameters = {
					new SqlParameter("@NodeId", SqlDbType.UniqueIdentifier,16)			};
            parameters[0].Value = NodeId;


            Bangle.Model.T_Node model = new Bangle.Model.T_Node();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["NodeId"].ToString() != "")
                {
                    model.NodeId = Guid.Parse(ds.Tables[0].Rows[0]["NodeId"].ToString());
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                model.RemindTime = ds.Tables[0].Rows[0]["RemindTime"].ToString();
                model.OrderTime = ds.Tables[0].Rows[0]["OrderTime"].ToString();
                model.CreateTime = ds.Tables[0].Rows[0]["CreateTime"].ToString();
                if (ds.Tables[0].Rows[0]["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
                }
                model.UpdateTime = ds.Tables[0].Rows[0]["UpdateTime"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateUserId"].ToString() != "")
                {
                    model.UpdateUserId = int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Valid"].ToString() != "")
                {
                    model.Valid = int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM T_Node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


    }
}

